/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ivanbiz.service;

import com.ivanbiz.dao.CashBalanceDAO;
import com.ivanbiz.dao.GLAccountDAO;
import com.ivanbiz.dao.impl.CashBalanceDAOImpl;
import com.ivanbiz.dao.impl.GLAccountDAOImpl;
import com.ivanbiz.model.CashBalance;
import com.ivanbiz.model.GLAccount;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Comboitem;
import org.zkoss.zul.Datebox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author iw20149x
 */
public class SaldoKasActionService extends SelectorComposer<Component> {

    @Wire
    Combobox listKreditur;
    @Wire
    Datebox datePosting;
    @Wire
    Textbox balance;
    @Wire
    Window winSaldoKasAction;
    CashBalanceDAO cashBalanceDAO;
    NumberFormat numberFormat;
    SimpleDateFormat sdf;
    GLAccountDAO gLAccountDAO;
    CashBalance cashBalance;

    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        cashBalanceDAO = new CashBalanceDAOImpl();
        gLAccountDAO = new GLAccountDAOImpl();
        renderGLAccount();
    }

    private void renderGLAccount() {
        try {
            List<GLAccount> loadGLAccounts = gLAccountDAO.getDataByEquals(GLAccount.class, "groupACC", "Kreditur");
            for (GLAccount gLAccount : loadGLAccounts) {
                Comboitem item = new Comboitem();
                item.setValue(gLAccount);
                item.setLabel(gLAccount.getNameGL() + " A/C No. " + gLAccount.getNoGL());
                listKreditur.appendChild(item);
            }
        } catch (Exception ex) {
            Logger.getLogger(SaldoKasActionService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Listen("onClick=#simpan;")
    public void simpan() {
        if (cashBalance == null) {
            cashBalance = new CashBalance();
        }
        cashBalance.setGlAccount((GLAccount) listKreditur.getSelectedItem().getValue());
        cashBalance.setDateBalance(new Date());
        cashBalance.setBalance(balance.getValue().isEmpty() ? (double) 0 : new Double(balance.getValue()));
        cashBalance.setStatus("1");
        validate(cashBalance);
    }

    private void validate(CashBalance cashBalance) {
        if (cashBalance.getGlAccount() == null) {
            Messagebox.show("Kreditur tidak boleh kosong");
        } else if (cashBalance.getBalance() == ((double) 0)) {
            Messagebox.show("Jumlah tidak boleh kosong");
        } else {
            try {
                if (cashBalanceDAO.validateSameDate(cashBalance.getGlAccount().getId(), cashBalance.getDateBalance())) {
                    Messagebox.show("Open Balance sudah dilakukan untuk tanggal " + sdf.format(cashBalance.getDateBalance()));
                } else {
                    cashBalanceDAO.saveOrUpdate(cashBalance);
                    winSaldoKasAction.detach();
                }
            } catch (Exception ex) {
                Logger.getLogger(SaldoKasActionService.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }
}
